﻿using UnityEngine;

public class MaterialFloatParamDriver :MonoBehaviour
{

	public string paramName;

	public float startVal;

	public float endVal;

	public float duration = 1;

	private float _currentTime = 0;

	private Material _material;

	void Start()
	{
		_material = GetComponent<Renderer>() != null ? GetComponent<Renderer>().sharedMaterial : null;
		if (duration == 0)
		{
			duration = 1;
		}
	}

	void OnEnable()
	{
		_currentTime = 0;
	}

	void LateUpdate()
	{
		if (_material != null && _material.HasProperty(paramName))
		{
			_currentTime += Mathf.Clamp(Time.smoothDeltaTime, 0, 0.0333f);
			float ratio = _currentTime / duration;
			if (ratio > 1)
			{
				ratio = 1;
				this.enabled = false;
			}
			_material.SetFloat(paramName, Mathf.Lerp(startVal, endVal, ratio));
		}
	}

	void OnDisable()
	{
		_currentTime = 0;
	}
}
